package com.xone.xapp;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class DownloadImagesTask extends AsyncTask<String, Integer, Bitmap> {
	
	protected static final String TAG = "DownloadImagesTask";

	@Override
	protected Bitmap doInBackground(String... params) {
		if (isCancelled()) {
			return null;
		}
		if (null == params || params.length == 0) {
			return null;
		}
		return downloadImage(params[0]);
	}
	
	protected Bitmap downloadImage(String url) {
	    Bitmap bitmap = null;
	    try {
	        URL aURL = new URL(url);
	        URLConnection conn = aURL.openConnection();
	        conn.connect();
	        InputStream is = conn.getInputStream();
	        BufferedInputStream bis = new BufferedInputStream(is);
	        bitmap = BitmapFactory.decodeStream(bis);
	        bis.close();
	        is.close();
	    } catch (IOException e) {
	        Log.e(TAG, "Error getting the image from server : " + e.getMessage().toString());
	    } 
	    return bitmap;
	}

}
